<script>

var ckfinder_url='<? echo BASE_URL?>js/ckfinder/';

$(document).ready(function(){
	$('.form').submit(function(){
		var error=false;
		var label;
		var object;
		$('.form .required').each(function(){
			label=$(this).closest('label').find('b').html();
			object=$(this);
			if($(this).is('textarea') && $(this).hasClass('editor'))
			{
				if($(this).attr('name'))
				{
					if(CKEDITOR.instances[$(this).attr('name')].getData()=='')
					{
						error=true;
						return false;
					}
				}
				else
				{
					alert('ckeditor is no name');
				}
			}
			else if($(this).is('textarea') || $(this).is("input[type='textbox']") || $(this).is("input[type='password']"))
			{
				if($(this).val()=='')
				{
					error=true;
					return false;
				}
			}
		})
		if(error)
		{
			alert(label+": thiếu thông tin");
			object.focus();
			return false;
		}
	});
						
	$('.editor').each(function(){
		var width=$(this).css('width');
		var height=$(this).css('height');			
		CKEDITOR.replace($(this)[0], { 
			height:height
			,width:width
			,toolbar:$(this).attr('toolbar')					
			,filebrowserBrowseUrl:ckfinder_url+'ckfinder.html'
			,filebrowserImageBrowseUrl:ckfinder_url+'ckfinder.html?type=images'
			,filebrowserFlashBrowseUrl:ckfinder_url+'ckfinder.html?type=flash'
			,filebrowserUploadUrl:ckfinder_url+'core/connector/php/connector.php?command=QuickUpload&type=files'
			,filebrowserImageUploadUrl:ckfinder_url+'core/connector/php/connector.php?command=QuickUpload&type=images'
			,filebrowserFlashUploadUrl:ckfinder_url+'core/connector/php/connector.php?command=QuickUpload&type=flash'
		});					
	})	
	
	$('.finder').each(function(){
		$(this).attr('readonly','readonly');
		$(this).after("<input type='button' value='Duyệt trên máy chủ' onclick=\"BrowseServer($(this).prev().attr('name'))\" />");					
	})
	
	<?php if($mode=='edit'){?>
	$('.i-update').each(function(index) {
		if($(this).attr('label')!=null)
		{
			var offset = $(this).offset();
			$(this).after("<span confirm='"+$(this).attr('confirm')+"' popup='"+$(this).attr('popup')+"' link='"+$(this).attr('link')+"' class='i-update' style='color:black;background-color:white;padding:2px 4px;border:1px solid #7c7c7c;cursor:pointer;position:absolute;left:"+offset.left+";top:"+offset.top+"'>"+$(this).attr('label')+"</span>");
			$(this).removeClass('i-update');
		}
		else
		{
			$(this).css('cursor','pointer');
			$(this).css('border','1px solid #7c7c7c');
			$(this).attr('title','Cập nhật nội dung này ['+$(this).attr('link')+']');
		}
		$('[title]',this).attr('title','');
		$(this).attr('onclick','').unbind('click');
	});
	$('.i-update').click(function(event){
		if($(this).attr('confirm')==1)//default is 0
		{
			if(!confirm('Bạn có đồng ý xóa không?'))
				return false;
		}
		if($(this).attr('popup')==0)//default is 1
			window.location='index.php?'+$(this).attr('link');
		else
			window.open('popup.php?'+$(this).attr('link'));
		return false;
	})
	$('.i-update').mouseenter(function(event){
		setShadow($(this),'0px 0px 3px 3px #3399ff');
		$(this).css('border-color','white');
	})
	$('.i-update').mouseleave(function(event){
		setShadow($(this),'0px 0px 0px 0px #7c7c7c');
		$(this).css('border-color','#7c7c7c');
	})
	<?php }?>
});

<?php if($mode=='edit'){?>
function setShadow(object,shadow)
{
	object.css('-moz-box-shadow',shadow);
	object.css('-webkit-box-shadow',shadow);	
	object.css('box-shadow',shadow);
}
<?php }?>

function SetFileField( fileUrl, data )
{	
	fileUrl=fileUrl.replace('<?php echo BASE_URL;?>','');
	$(".finder[name='"+data["selectActionData"]+"']").val(fileUrl);
}

function BrowseServer( inputName )
{
	if(inputName)
	{
		var finder = new CKFinder() ;
		finder.BasePath = ckfinder_url ;
		finder.SelectFunction = SetFileField ;
		finder.SelectFunctionData = inputName;
		finder.Popup() ;
	}
	else
	{
		alert('ckfinder is no name');
	}
}

</script>